package com.cartrentalsystem.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cartrentalsystem.domain.po.Customers;
import com.cartrentalsystem.mapper.CustomersMapper;
import com.cartrentalsystem.service.ICustomersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cartrentalsystem.utils.JwtUtil;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.util.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LHD
 * @since 2025-06-11
 */
@Service
@Slf4j
public class CustomersServiceImpl extends ServiceImpl<CustomersMapper, Customers> implements ICustomersService {

    @Override
    public Page<Customers> pageList(Integer pageNum, Integer pageSize, String name, String phone, String cartNumber, Integer status){
        Page<Customers> page = new Page<>(pageNum, pageSize);
        return this.lambdaQuery()
                .eq(Customers::getDeleted, 0)
                .like(name != null, Customers::getName, name)
                .eq(phone != null, Customers::getPhone, phone)
                .eq(cartNumber != null, Customers::getCartNumber, cartNumber)
                .eq(status != null, Customers::getStatus, status)
                .orderByAsc(Customers::getStatus)
                .page(page);
    }

    @Override
    @Transactional
    public boolean saveCustomer(Customers customers){
        if (customers.getId()!=null){
            // 此时为编辑
            this.lambdaUpdate()
                    .eq(Customers::getId, customers.getId())
                    .update(customers);
                    log.info("用户编辑成功");
                    return true;
        }
        log.info("添加用户");
        customers.setRentalTimes(0);
        LocalDate  now = LocalDate.now();
        customers.setLatelyRentalTime(now);
        customers.setDeleted(0);
        return this.save(customers);
    }

    @Override
    public boolean updateCustomer(Customers customers){
        return this.lambdaUpdate()
                .eq(Customers::getId, customers.getId())
                .set(customers.getName()!=null&& !customers.getName().isEmpty(),Customers::getName, customers.getName())
                .set(customers.getPhone()!=null,Customers::getPhone, customers.getPhone())
                .set(customers.getCartNumber()!=null,Customers::getCartNumber, customers.getCartNumber())
                .set(customers.getStatus()!=null,Customers::getStatus, customers.getStatus())
                .update();
    }

}
